<?
/*
-------------------------------
Script - EvaGb
-------------------------------
File - editonepost.php
-------------------------------
Function - Модуль админцентра, изменение сообщения
-------------------------------
*/
include_once "{$GLOBALS['_gb_cnf']['dir']}/sis/inc/replace_inc.php";
class admin_modul {
    public  $error,$result;
    private $db,$tpl,$adm,$id;
    function __construct() {
        debug();
        $this->db=locator::get()->db();
        $this->tpl=locator::get()->tpl();
        $this->adm=locator::get()->save['admin'];
        $this->id=filter($_GET['id'],'0-9');
        $_SESSION['edpo_refer']=!$_SESSION['edpo_refer']?$_SESSION['page_refer']:$_SESSION['edpo_refer'];
        if ($this->id) {
          if (!$this->user_request()) {
              $this->result=$this->tpl();
          }
        }else {
           $this->error="Ошибка: Сообщение не найдено";
        }
    }
    private function user_request() {
        if (issetpost("name,text".($GLOBALS['_gb_cnf']['addfild']?",".implode(",",$GLOBALS['_gb_cnf']['addfild']):''))) {
            if (!emptypost("name,text".($GLOBALS['_gb_cnf']['noempty']?",".implode(",",$GLOBALS['_gb_cnf']['noempty']):''))) {
               if ($this->check_regexp()) {
                   $this->update();
                   return true;
               }
            }else {
                $this->error="Ошибка: Обязательные поля нужно заполнить";
            }
        }
        return false;
    }
    private function update() {
       $arr=givepost("name,text");
       $arr['name']=mysql_real_escape_string(mreplace($arr['name']));
       $arr['text']=mysql_real_escape_string(replace($arr['text']));
       if ($GLOBALS['_gb_cnf']['addfild']) $arr['addfilds']=serialform($GLOBALS['_gb_cnf']['addfild']);
       if ($this->adm->updatepost($this->id,$arr)) {
           $this->error="Сообщение успешно отредактирвано";
           header("Refresh:2;url={$_SESSION['edpo_refer']}");
           $_SESSION['edpo_refer']=false;
       }else {
           $this->error=$this->adm->error;
       }
    }
    private function check_regexp() {
        $reg=$GLOBALS['_gb_cnf']['regexp'];
        foreach($reg as $key=>$val) {
            if (!preg_match($val['regexp'],$_POST[$key])) {
                $this->error=$val['error'];
                return false;
            }
        }
        return true;
    }
    private function sql() {
        return $this->db->query("
                       SELECT * FROM {$this->db->pref}_post WHERE id_post='{$this->id}'
                       ");
    }

    private function tpl() {
        $sql=$this->sql();
        if (mysql_num_rows($sql)) {
           $row=$this->db->fetch_array($sql);
           $row['addfilds']=unserialize($row['addfilds']);
           $row['text']=r_replace($row['text']);
        }
        $this->tpl->set('result',$row);
        $this->tpl->set('bbcode',$this->tpl->load("admin_tpl/admin_bbcode.tpl"));
        return $this->tpl->load("admin_tpl/editonemsg.tpl");
    }
}

?>
